import { simpleThrottle } from '../utils/utils'

// earth 初始化
function earthInit(el) {
	let viewer = new GV.GeoCanvas(el, {
		shouldAnimate: true, //启用动画
		// timeline: true,
		// animation: true,
		baseLayerPicker: false,
		skyBox: new Cesium.SkyBox({
			sources: {
				positiveX: 'images/SkyBox/mx.jpg',
				negativeX: 'images/SkyBox/mx.jpg',
				positiveY: 'images/SkyBox/py.jpg',
				negativeY: 'images/SkyBox/my.jpg',
				positiveZ: 'images/SkyBox/pz.jpg',
				negativeZ: 'images/SkyBox/mz.jpg',
			},
		}),
	})
	let options = {}
	// 用于在使用重置导航重置地图视图时设置默认视图控制。接受的值是Cesium.Cartographic 和Cesium.Rectangle.
	options.defaultResetView = Cesium.Cartographic.fromDegrees(110, 30, 2000000)
	// 用于启用或禁用罗盘。true是启用罗盘，false是禁用罗盘。默认值为true。如果将选项设置为false，则罗盘将不会添加到地图中。
	options.enableCompass = true
	// 用于启用或禁用缩放控件。true是启用，false是禁用。默认值为true。如果将选项设置为false，则缩放控件 将不会添加到地图中。
	options.enableZoomControls = true
	// 用于启用或禁用距离图例。true是启用，false是禁用。默认值为true。如果将选项设置为false，距离图例将不会添加到地图中。
	options.enableDistanceLegend = true
	// 用于启用或禁用指南针外环。true是启用，false是禁用。默认值为true。如果将选项设置为false，则该环将可见但无效。
	options.enableCompassOuterRing = true
	options.mapMode2D = Cesium.MapMode2D.ROTATE

	viewer.extend(Cesium.viewerCesiumNavigationMixin, options)

	// this.viewer.scene.morphTo2D(0); //切换二维
	viewer.scene.debugShowFramesPerSecond = true //显示帧率

	// 设置底图
	let baseImageLayer = viewer.imageryLayers.addImageryProvider(
		new Cesium.UrlTemplateImageryProvider({
			url: 'http://mt1.google.cn/vt/lyrs=s&hl=zh-CN&x={x}&y={y}&z={z}&s=Gali',
		})
	)

	window.viewer = viewer

	setGlobalHandler()
}
// 设置cesium全局事件
function setGlobalHandler() {
	// 注册cesium全局事件
	const leftClickHandler = new Cesium.ScreenSpaceEventHandler(viewer.canvas)
	const mousemoveHandler = new Cesium.ScreenSpaceEventHandler(viewer.canvas)
	leftClickHandler.setInputAction(function(movement) {
		const pickedObject = viewer.scene.pick(movement.position)
		// ie中通过GraphicLayer加载的对象可以通过该方法拾取
		// const pickedObject = that.viewer.graphicLayer.pickByCoordinate(movement.position.x, movement.position.y)
		if (Cesium.defined(pickedObject) && Cesium.defined(pickedObject.id) && typeof pickedObject.id.leftClick === 'function') {
			pickedObject.id.leftClick(pickedObject.id)
		}
	}, Cesium.ScreenSpaceEventType.LEFT_CLICK)

	var simpleThrottleFunc = simpleThrottle(mouseMoveEvent, 100, this)
	mousemoveHandler.setInputAction(function mouseMove(movement) {
		simpleThrottleFunc(movement)
	}, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
}
// cesium鼠标浮动事件
function mouseMoveEvent(movement) {
	const pickedObject = viewer.scene.pick(movement.endPosition)
	const iepickedObject = viewer.graphicLayer.pickByCoordinate(movement.endPosition.x, movement.endPosition.y)
	// entity 如果包含属性cursor，鼠标浮动时变小手
	if ((Cesium.defined(pickedObject) && Cesium.defined(pickedObject.id) && pickedObject.id.cursor === true) || (iepickedObject && iepickedObject.cursor)) {
		viewer.canvas.style.cursor = 'pointer'
	} else {
		viewer.canvas.style.cursor = 'default'
	}
	// 如果为该entity添加了鼠标浮动事件，调用该方法
	// 所有infoBox都绑定 vuex 中的infoBoxShow，在鼠标浮动时，先将其设置为false

	// store.dispatch("layerManage/setInfoBoxShowFun", false);
	if (Cesium.defined(pickedObject) && Cesium.defined(pickedObject.id) && typeof pickedObject.id.mouseMove === 'function') {
		pickedObject.id.mouseMove(pickedObject.id, movement.endPosition)
	}
	// iepickedObject 的判断
	if (iepickedObject && iepickedObject.mouseMove && typeof iepickedObject.mouseMove === 'function') {
		iepickedObject.mouseMove(iepickedObject, movement.endPosition)
	}
}
export { earthInit }
